package com.jiashihui.web.controller;

import com.jiashihui.web.model.test.User;
import com.jiashihui.web.service.UserService;
import com.jiashihui.web.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * @author jiashihui
 * @describe: 测试
 * @date 2019/01/07
 */
@RestController
@RequestMapping("redis")
public class RedisController {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserService service;

    @GetMapping(value = "/getStr", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getStr(String key){
        return redisUtil.get(key) == null ? key + " : null" : redisUtil.get(key).toString();
    }

    @PostMapping(value = "/save", consumes = { MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public User saveUser(@RequestBody User user){
        return service.saveUser(user);
    }

    @GetMapping(value = "/getUser/{userId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public User getUser(@PathVariable Integer userId){
        return service.getUserById(userId);
    }

    @DeleteMapping(value = "/delete/{userId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String deleteUser(@PathVariable Integer userId){
        return service.removeUserById(userId) ? "success" : "fail";
    }
}
